package com.mengfh.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mengfh.domain.Dep;
import com.mengfh.domain.Emp;
import com.mengfh.service.IDepService;
import com.mengfh.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 此处没有处理严谨逻辑， 仅为前端页面进行数据展示
 * @Author mengfh
 * @Date 15/6/23 上午6:55
 * @Version V1.0
 **/

@RestController
@CrossOrigin
public class HomeController {


    @Autowired
    IDepService depService;

    @Autowired
    IEmpService empService;


    /**
     * 功能测试
     *
     * @return
     */
    @RequestMapping("/")
    Map sayHi() {

        final HashMap hashMap = new HashMap();
        hashMap.put("code", 200);
        hashMap.put("msg", "你好");

        return hashMap;

    }


    /**
     * 导入测试部门
     *
     * @return
     */
    @RequestMapping("/addDep/test")
    String add() {

        List<Dep> list = new ArrayList<>();
        Dep dep = new Dep();
        dep.setDepName("科技部");
        list.add(dep);

        Dep dep2 = new Dep();
        dep2.setDepName("人事部");
        list.add(dep2);


        Dep dep3 = new Dep();
        dep3.setDepName("财务部");
        list.add(dep3);


        depService.saveBatch(list);

        return "ok";
    }


    /**
     * 导入测试员工
     *
     * @return
     */
    @RequestMapping("/addEmp/test")
    public String addEmp() {

        List<Emp> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            final Emp emp = new Emp();
            emp.setEmpName(UUID.randomUUID().toString());
            emp.setAge(20);
            emp.setHireDate(new Date());
            emp.setDepId(1L);
            list.add(emp);
        }
        empService.saveBatch(list);


        return "ok";
    }


    /**
     * 分页获取员工列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/listEmp")
    public Map listEmp(Integer pageIndex, Integer pageSize) throws JsonProcessingException {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }

        Page pageParm = new Page();
        pageParm.setCurrent(pageIndex);
        pageParm.setSize(pageSize);
        final Page pageResult = empService.page(pageParm, Wrappers.lambdaQuery(Emp.class).orderByDesc(Emp::getEmpId));

        List<Emp> records = pageResult.getRecords();
        records = records.stream().map(e -> {
            final Dep byId = depService.getById(e.getDepId());
            e.setDep(byId);
            return e;
        }).collect(Collectors.toList());

        Map retMap = new HashMap();
        retMap.put("code", 200);
        retMap.put("msg", "成功");

        JSONObject obj = new JSONObject();
        obj.putOnce("data", records).putOnce("count", pageResult.getTotal());
        retMap.put("obj", obj);


        return retMap;
    }


    /**
     * 获取部门列表
     *
     * @return
     */
    @RequestMapping("/listDep")
    public Map listDep() {

        final List<Dep> list = depService.list();


        Map retMap = new HashMap();
        retMap.put("code", 200);
        retMap.put("msg", "成功");

        JSONObject obj = new JSONObject();
        obj.putOnce("data", list).putOnce("count", list.size());
        retMap.put("obj", obj);


        return retMap;
    }


    /**
     * 添加员工
     *
     * @param emp
     * @return
     */
    @RequestMapping("/addEmp")
    public Map addEmp(@RequestBody Emp emp) {

        empService.save(emp);

        Map retMap = new HashMap();
        retMap.put("code", 200);
        retMap.put("msg", "成功");

        JSONObject obj = new JSONObject();
        obj.putOnce("data", null);

        return retMap;

    }


    /**
     * 删除员工
     *
     * @param empId
     * @return
     */
    @RequestMapping("/delEmp")
    public Map delEmp(Long empId) {

        empService.removeById(empId);
        Map retMap = new HashMap();
        retMap.put("code", 200);
        retMap.put("msg", "成功");

        JSONObject obj = new JSONObject();
        obj.putOnce("data", null);

        return retMap;


    }


    /**
     * 更新员工
     *
     * @param emp
     * @return
     */
    @RequestMapping("/updateEmp")
    public Map updateEmp(@RequestBody Emp emp) {

        empService.saveOrUpdate(emp);
        Map retMap = new HashMap();
        retMap.put("code", 200);
        retMap.put("msg", "成功");

        JSONObject obj = new JSONObject();
        obj.putOnce("data", null);

        return retMap;


    }


}